#!/bin/sh
#Auth:anbingyu
#Date:20210421
#About:install  Java jdk1.8.0_231
#Version:1.0.0
#java安装包
JAVA_FILE="jdk-8u231-linux-x64.tar.gz"
#java解压之后名称
JAVA_VERSION="jdk1.8.0_231"
#java安装目录
JAVA_PATH="/usr/jdk64/"

function remove_oepnjdk() {
	##delete openjdk
	echo "删除openjdk"
	rpm -qa|grep java |xargs -n 1 rpm -e --nodeps >>/dev/null
	}
function check_environment () {
        echo "安装相关依赖"
	yum -y install gcc gcc-c++ autoconf automake libtool make zlib-devel openssl-devel pcre-devel p7zip >>/dev/null
	##创建目录
	mkdir -p $JAVA_PATH
}		
function java_check() {
	if [ -f $JAVA_FILE ];
	   then
		 tar xf $JAVA_FILE && mv $JAVA_VERSION $JAVA_PATH
	   else
		 echo -e "plase upload $JAVA_FILE "
		 exit 0
	  fi
}		
function java_install (){
	chown -R root.root $JAVA_PATH$JAVA_VERSION
	chmod -R 755 $JAVA_PATH$JAVA_VERSION
	echo "##JDK1.8 env" >> /etc/profile.d/jdk.sh
	echo "export JAVA_HOME=/usr/jdk64/$JAVA_VERSION" >> /etc/profile.d/jdk.sh
	echo "export CLASSPATH=\$JAVA_HOME/lib/rt.jar:\$JAVA_HOME/lib/ext" >> /etc/profile.d/jdk.sh
	echo "export PATH=.:\$PATH:\$JAVA_HOME/bin" >> /etc/profile.d/jdk.sh
	source /etc/profile
	#创建软链
	ln -s $JAVA_PATH$JAVA_VERSION/bin/java /usr/bin/
	echo "$JAVA_VERSION环境部署完成"
	echo "安装目录:$JAVA_PATH"
	echo "安装版本:"
	java -version
	echo "环境变量：/etc/profile.d/jdk.sh"
}

remove_oepnjdk
check_environment
java_check
java_install
